package com.sgb.kjwl.view.ui.fragment.material;

import androidx.databinding.DataBindingUtil;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.Nullable;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentGoodsAllBinding;
import com.sgb.kjwl.view.ui.adapter.CommPagerAdapter2;
import com.swgk.core.view.entity.MenuEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.LeaseEvent;
import com.sgb.kjwl.view.ui.activity.home.HomeCategoryActivity;
import com.sgb.kjwl.view.ui.fragment.HomeFragment3;
import com.swgk.core.base.BaseFragment;
import com.swgk.core.base.model.entity.RequestCode;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 所有商品fragment
 */
public class GoodsBaseFragment extends AppFragment {
    private FragmentGoodsAllBinding binding;
    private ArrayList<BaseFragment> fragmentList = new ArrayList<>();
    private List<MenuEntity> data;
    private List<MenuEntity> goodsType = new ArrayList<>();
    private List<MenuEntity> goodsTypeCx = new ArrayList<>();
    private CommPagerAdapter2 commPagerAdapter2;
    private CommPagerAdapter2 commPagerAdapter3;
    private HomeFragment3 homeFragment;
    private static final String BUNDLE_FRAGMENTS_KEY = "android:support:fragments";
    private String id;
    private String nameId;
    private String threeLevelClassId;
    private boolean isCreate = false;
    private boolean init = true;
    private boolean isVisible;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = isVisibleToUser;
        if (!isCreate) {
            return;
        }
        if (isVisibleToUser) {
            if (init) {
                init = false;
                initView();
            }
        }
    }

    public static GoodsBaseFragment newInstance(Object goodsTypeList, String id, int position) {
        GoodsBaseFragment fragment = new GoodsBaseFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("goodsTypeList", (Serializable) goodsTypeList);
        bundle.putString("id", id);
        bundle.putInt("position", position);
        fragment.setArguments(bundle);
        return fragment;
    }

    public static GoodsBaseFragment newInstance(Object goodsTypeList, String id, int position, String threeLevelClassId, String nameId) {
        GoodsBaseFragment fragment = new GoodsBaseFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("goodsTypeList", (Serializable) goodsTypeList);
        bundle.putString("id", id);
        bundle.putInt("position", position);
        bundle.putString("threeLevelClassId", threeLevelClassId);
        bundle.putString("nameId", nameId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        isCreate = true;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_goods_all, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (init && isVisible) {
            init = false;
            initView();
        }
    }

    public void setCurrentItem(int position, String threeLevelClassId, String nameId) {
        this.threeLevelClassId = threeLevelClassId;
        this.nameId = nameId;
        goodsTypeCx.clear();
        goodsType.clear();
        goodsType.addAll(data);
        goodsTypeCx.addAll(data);
        fragmentList.clear();
        if ("29".equals(id)) {
            initAllFragment();
            commPagerAdapter2 = new CommPagerAdapter2(getChildFragmentManager(), fragmentList, goodsType);
            binding.viewpager.setAdapter(commPagerAdapter2);
            binding.tablayout.setupWithViewPager(binding.viewpager);
            binding.viewpager.setCurrentItem(position);
        } else if ("30".equals(id)) {
            initCxFragment();
            commPagerAdapter3 = new CommPagerAdapter2(getChildFragmentManager(), fragmentList, goodsTypeCx);
            binding.viewpager2.setAdapter(commPagerAdapter3);
            binding.tablayout.setupWithViewPager(binding.viewpager2);
            binding.viewpager2.setCurrentItem(position);
        }

    }

    private void initView() {
        data = (List<MenuEntity>) getArguments().getSerializable("goodsTypeList");
        id = getArguments().getString("id");
        int position = getArguments().getInt("position", 0);
        threeLevelClassId = getArguments().getString("threeLevelClassId");
        nameId = getArguments().getString("nameId");
        binding.imgType.setOnClickListener(this);
        binding.tvXj.setOnClickListener(this);
        binding.allType.setOnClickListener(this);
        goodsType.clear();
        goodsTypeCx.clear();
        goodsType.addAll(data);
        goodsTypeCx.addAll(data);
        fragmentList.clear();
        homeFragment = new HomeFragment3();

        if ("29".equals(id)) {
            initAllFragment();
            commPagerAdapter2 = new CommPagerAdapter2(getChildFragmentManager(), fragmentList, goodsType);
            binding.viewpager.setAdapter(commPagerAdapter2);
            binding.tablayout.setupWithViewPager(binding.viewpager);
            binding.viewpager.setCurrentItem(position);
        } else if ("30".equals(id)) {
            initCxFragment();
            commPagerAdapter3 = new CommPagerAdapter2(getChildFragmentManager(), fragmentList, goodsTypeCx);
            binding.viewpager2.setAdapter(commPagerAdapter3);
            binding.tablayout.setupWithViewPager(binding.viewpager2);
            binding.viewpager2.setCurrentItem(position);
        }

    }

    /**
     * 所有商品
     */
    private void initAllFragment() {
        for (int i = 0; i < goodsType.size(); i++) {
            if (goodsType.get(i).getId().equals("7")) {
                //商砼
                fragmentList.add(AllStFragment3.start( goodsType.get(i).getId(), goodsType.get(i).getId(), threeLevelClassId));
            } else if (!goodsType.get(i).getName().equals("全部")) {
                //商品
                fragmentList.add(AllMaterialListFragment3.start(goodsType.get(i).getName(), "0", goodsType.get(i).getId(), 0, threeLevelClassId, nameId));
            } else {
                fragmentList.add(SelectAllMaterialListFragment3.start(0));
            }
        }
    }

    /**
     * 促销
     */
    private void initCxFragment() {
        for (int i = 0; i < goodsTypeCx.size(); i++) {
            if (goodsTypeCx.get(i).getId().equals("7")) {
                //商砼
                goodsTypeCx.remove(i);
            }
        }
        for (int i = 0; i < goodsTypeCx.size(); i++) {
            if (!goodsType.get(i).getName().equals("全部")) {
                //商品
                fragmentList.add(AllMaterialListFragment3.start(goodsTypeCx.get(i).getName(), "0", goodsTypeCx.get(i).getId(), 1));
            } else {
                fragmentList.add(SelectAllMaterialListFragment3.start(1));
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_type:
            case R.id.all_type:
                HomeCategoryActivity.start(getActivity(), RequestCode.REQUEST_SHOP);
                break;
            case R.id.tv_xj:
                homeFragment.goLogin(getActivity());
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LeaseEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_SHOP_VISIBLE) {
            binding.layoutBottom.setVisibility(View.VISIBLE);
        } else if (event.getMessageTag() == EventConstant.EVENT_SHOP_GONE) {
            binding.layoutBottom.setVisibility(View.GONE);
        }
    }

    @SuppressLint("MissingSuperCall")
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (outState != null ) {
            //销毁时不保存fragment的状态
            outState.remove(BUNDLE_FRAGMENTS_KEY);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}

